package cn.zyq.ykb.service.course.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.course.CourseChartMapper;
import cn.zyq.ykb.beans.course.CourseChart;
import cn.zyq.ykb.service.course.CourseChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程-轮播图Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-06-01
 */
@Service
public class CourseChartServiceImpl  extends BaseService implements CourseChartService {

    @Autowired
    private CourseChartMapper courseChartMapper;

    /**
     * 查询课程-轮播图
     *
     * @param courseChartID 课程-轮播图主键
     * @return 课程-轮播图
     */
    @Override
    public CourseChart selectCourseChartByCourseChartID(Integer courseChartID) {
        CourseChart courseChart = courseChartMapper.selectByPrimaryKey(courseChartID);
        if (courseChart.getDeleted() == 1) {
            return null;
        }
        return courseChart;
    }

    /**
     * 查询课程-轮播图
     *
     * @param serviceID 业务ID
     * @return 课程-轮播图
     */
    @Override
    public CourseChart selectCourseChartByServiceID(String serviceID) {
        CourseChart param = new CourseChart();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.courseChartMapper.selectOne(param);
    }

    /**
     * 查询课程-轮播图列表
     *
     * @param pageParam
     * @return 课程-轮播图
     */
    @Override
    public PageInfo<CourseChart> selectCourseChartList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<CourseChart> list = this.courseChartMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询课程-轮播图列表
     *
     * @param param
     * @return 课程-轮播图
     */
    @Override
    public List<CourseChart> selectCourseChartList(CourseChart param) {
        param.setDeleted(0);
        return this.courseChartMapper.select(param);
    }
    /**
     * 新增课程-轮播图
     *
     * @param param 课程-轮播图
     * @return 结果
     */
    @Override
    public int insertCourseChart(CourseChart param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.courseChartMapper.insertSelective(param);
    }

    /**
     * 修改课程-轮播图
     *
     * @param param 课程-轮播图
     * @return 结果
     */
    @Override
    public int updateCourseChart(CourseChart param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.courseChartMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除课程-轮播图
     *
     * @param courseChartIDs 需要删除的课程-轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCourseChartByCourseChartIDs(Integer[] courseChartIDs) {
        int i = 0;
        for (Integer courseChartID : courseChartIDs) {
            i += this.deleteCourseChartByCourseChartID(courseChartID);
        }
        return i;
    }

    /**
     * 删除课程-轮播图信息
     *
     * @param courseChartID 课程-轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCourseChartByCourseChartID(Integer courseChartID) {
        CourseChart courseChart = this.selectCourseChartByCourseChartID(courseChartID);
        if (courseChart == null) {
            return 0;
        } else {
            CourseChart storeInfo = new CourseChart();
            storeInfo.setCourseChartID(courseChartID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.courseChartMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
